{-------------------------------------------------------------------------------
  Author: Duo (iamduo@qq.com)
  DateTime: 2019/7/13 23:01:17

  FireDAC.TFDParams
-------------------------------------------------------------------------------}
unit uDBParam_FDParams;

interface

uses uDB_Base, uBizDBIntf, SysUtils, FireDAC.Stan.Param, Variants;

type
  TDBParam_Param = class(TDBLockedBase, IDBParam)
  private
    FDParam:TFDParam;
  public
    constructor Create(const ADBParamsIntf:IDBParams; const AFDParam:TFDParam);
    destructor Destroy;override;
    { IDBParam }
    function IsBlob:Boolean;
    function IsNull:Boolean;
    function Name:WideString;
    function Kind:WideString;
    function Size:Integer;
    function GetValue:OleVariant;
    procedure SetValue(const AValue:OleVariant);
    property Value:OleVariant read GetValue write SetValue;
  end;

  TDBParam_FDParams = class(TDBLockedBase, IDBParams)
  private
    FDParams:TFDParams;
  public
    constructor Create(const ADBDataIntf:IDBData; const AFDParams:TFDParams);
    destructor Destroy;override;
    { IDBParams }
    function Count:Integer;
    function Add:Integer;
    function Delete(const AIndex:Integer):Boolean;
    function GetParams(const AIndex:Integer):IDBParam;
    procedure SetParams(const AIndex:Integer; const AValue:IDBParam);
    property Params[const AIndex:Integer]:IDBParam read GetParams write SetParams;default;
  end;

implementation

uses Classes;

{ TDBParam_Param }

constructor TDBParam_Param.Create(const ADBParamsIntf: IDBParams;
  const AFDParam: TFDParam);
begin
  inherited Create(ADBParamsIntf);
  FDParam := AFDParam;
end;

destructor TDBParam_Param.Destroy;
begin
  FDParam := nil;
  inherited;
end;

function TDBParam_Param.GetValue: OleVariant;
begin
  Result := FDParam.Value;
end;

function TDBParam_Param.IsBlob: Boolean;
begin
  Result := FieldType_IsBlob(FDParam.DataType);
end;

function TDBParam_Param.IsNull: Boolean;
begin
  Result := VarIsNull(FDParam.Value);
end;

function TDBParam_Param.Kind: WideString;
begin
  Result := FieldType_ToString(FDParam.DataType);
end;

function TDBParam_Param.Name: WideString;
begin
  Result := FDParam.Name;
end;

procedure TDBParam_Param.SetValue(const AValue: OleVariant);
begin
  FDParam.Value := AValue;
end;

function TDBParam_Param.Size: Integer;
begin
  Result := FDParam.Size;
end;

{ TDBParam_FDParams }

function TDBParam_FDParams.Add: Integer;
begin
  Lock;
  try
    Result := FDParams.Count;
    FDParams.Add;
  finally
    UnLock;
  end;
end;

function TDBParam_FDParams.Count: Integer;
begin
  Lock;
  try
    Result := FDParams.Count;
  finally
    UnLock;
  end;
end;

constructor TDBParam_FDParams.Create(const ADBDataIntf: IDBData;
  const AFDParams: TFDParams);
begin
  inherited Create(ADBDataIntf);
  FDParams := AFDParams;
end;

function TDBParam_FDParams.Delete(const AIndex: Integer): Boolean;
begin
  Lock;
  try
    Result := false;
    if (AIndex >= 0) and (AIndex < FDParams.Count) then
    begin
      FDParams.Delete(AIndex);
      Result := true;
    end;
  finally
    UnLock;
  end;
end;

destructor TDBParam_FDParams.Destroy;
begin
  FDParams := nil;
  inherited;
end;

function TDBParam_FDParams.GetParams(const AIndex: Integer): IDBParam;
begin
  Lock;
  try
    Result := TDBParam_Param.Create(Self, FDParams[AIndex]);
  finally
    UnLock;
  end;
end;

procedure TDBParam_FDParams.SetParams(const AIndex: Integer;
  const AValue: IDBParam);
begin
  Lock;
  try
    FDParams[AIndex].Name := AValue.Name;
    FDParams[AIndex].DataType := FieldType_FromString(AValue.Kind);
    FDParams[AIndex].Size := AValue.Size;
    FDParams[AIndex].Value := AValue.Value;
  finally
    UnLock;
  end;
end;

end.

